import requests

server_url="http://154.64.255.89:6543/upload"
def upload_image(file_path, password):
    """
    上传图片到服务器（需要密码）
    :param file_path: 图片文件路径
    :param password: 上传密码
    :param server_url: 上传接口URL
    :return: 服务器返回的图片URL或错误信息
    """
    try:
        # 检查文件是否存在
        with open(file_path, 'rb') as f:
            files = {'file': (file_path.split('/')[-1], f, 'image/jpeg')}
            data = {'password': password}
            
            # 发送POST请求
            response = requests.post(server_url, files=files, data=data)
            
            # 处理响应
            if response.status_code == 200:
                result = response.json()
                return result['url']
            else:
                return 1
    
    except FileNotFoundError:
        return 1
    except Exception as e:
        return 1

if __name__ == "__main__":
    # 使用示例
    image_path = "XXX.jpg"  # 替换为你的图片路径
    upload_password = "XXXXXXXXXXXXXXXXXXXX"  # 与服务端设置的密码一致
    
    result = upload_image(image_path, upload_password)
    print(result)